//vue
import Vue from 'vue';
//app.vue
import app from './app.vue'
import Vuex from 'vuex'
Vue.use(Vuex);

//导入mui
import './lib/dist/css/mui.min.css'
//导入路由
import VueRouter from 'vue-router';
Vue.use(VueRouter);
import router from './router.js'

//导入额外的字体图标
import './lib/dist/css/icons-extra.css'
import './lib/dist/css/mui.picker.css'
import './lib/dist/css/mui.poppicker.css'

// 2.1 导入 vue-resource
import VueResource from 'vue-resource'
// 2.2 安装 vue-resource
Vue.use(VueResource);
//import moment from 'moment'
/*import  mui from  './lib/dist/js/mui.js'
Vue.prototype.mui = mui*/
import { Picker } from 'mint-ui';

Vue.component(Picker.name, Picker);

import VueScroller from "vue-scroller"
Vue.use(VueScroller);
 var user = JSON.parse(localStorage.getItem('user')||'[]');
var email = JSON.parse(localStorage.getItem('email')||'[]');
var id = JSON.parse(localStorage.getItem('id')||'[]');
var cart = JSON.parse(localStorage.getItem('car')||'[]');
const store = new Vuex.Store({
    state:{
        id:'',
        user:user,
        email:email,
        user_id:id,
        cart:cart
    },
    mutations:{
        getId(state,id){
            state.id = id;
        },
        getUser(state,option){
            localStorage.setItem('user',JSON.stringify(option.user));
            localStorage.setItem('email',JSON.stringify(option.email));
            localStorage.setItem('id',JSON.stringify(option.id));
        },
        deleteCart(state,id){
            state.cart = JSON.parse(localStorage.getItem('cart')||'[]');
            state.cart.forEach((item,i)=>{
                if(item.id == id){
                    state.cart.splice(i,1);
                }
            });
            localStorage.setItem('cart',JSON.stringify(state.cart));
        }
    },
    getters:{
        getUser:function (state) {
            return state.user;
        },
        //得到总计数量和总计价格
        //并没有考虑购物车界面checkbox是否选中的更新操作
        getTotal(state){
            var obj = {
                countTotal:0,
                priceTotal:0
            };
            state.cart = JSON.parse(localStorage.getItem('cart')||'[]');
            state.cart.forEach((item)=>{
                if(item.selected){
                    obj.countTotal += item.count;
                    obj.priceTotal += item.price * item.count;
                }
            });
            return obj;
        }
    }

});
var vm = new Vue({
    el:'#app',
    data:{
        msg:'123',
    },
    render:function (c) {
        return c(app);
    },
    router:router,
    store,
});


